package com.gsx.service.impl;

import com.gsx.bean.BasicInfo;
import com.gsx.bean.Cart;
import com.gsx.bean.Good;
import com.gsx.bean.InfoDetail;
import com.gsx.dao.BasicInfoMapper;
import com.gsx.dao.CartMapper;
import com.gsx.dao.GoodMapper;
import com.gsx.dao.InfoDetailMapper;
import com.gsx.service.GoodService;
import com.gsx.service.InfoDetailService;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @Authoe gsx
 * @Date 2021/04/07 上午 9:58
 **/
@Service
public class InfoDetailServiceImpl implements InfoDetailService {
    @Resource
    private InfoDetailMapper infoDetailMapper;
    @Resource
    private GoodMapper goodMapper;
    @Resource
    private BasicInfoMapper basicInfoMapper;
    @Override
    public List<InfoDetail> getListByGinAndBIid(int gid, int bIId) {
            return infoDetailMapper.selectListByGinAndBIid(gid, bIId);
    }

    @Override
    public Cart setCart(int sum, int gid,int uid,int[] infoDetailId) {
        List<InfoDetail> infoDetails=null;
        Cart cart =setCart(sum, gid);
        if (uid!=-1){
          cart.setFkCartUserId(uid);
        }
        for (int i=0;i<infoDetailId.length;i++){
            InfoDetail infoDetail = new InfoDetail();
            infoDetail.setFkBasicInfoId(infoDetailId[i]);
            if (i==0){
                infoDetails=new ArrayList<>();
            }
            infoDetails.add(infoDetail);
        }
        cart.setInfoDetails(infoDetails);
        return cart;
    }

    @Override
    public List<BasicInfo> getListByGin(int gid) {
        return basicInfoMapper.selectListByGin(gid);
    }

    private  Cart setCart(int sum, int gid) {
        Good good = goodMapper.selectByPrimaryKey(gid);
        Cart cart = new Cart();
        cart.setFkCartGoodId(good.getGoodId());
        cart.setCartGoodPrice(good.getGoodCost()-sum);
        return cart;
    }
}
